package com.mdd.admin.service.unit.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.mdd.admin.service.unit.IGoodsUnitService;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.validate.unit.GoodsUnitParam;
import com.mdd.admin.vo.unit.GoodsUnitDetailVo;
import com.mdd.admin.vo.unit.GoodsUnitListVo;
import com.mdd.common.constant.GlobalConstant;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.goods.GoodsUnit;
import com.mdd.common.mapper.unit.GoodsUnitMapper;
import com.mdd.common.utils.TimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 服务单位实现类
 */
@Service
public class GoodsUnitServiceImpl extends ServiceImpl<GoodsUnitMapper, GoodsUnit> implements IGoodsUnitService {

    @Resource
    GoodsUnitMapper goodsUnitMapper;

    /**
     * 服务单位列表
     *
     * @param pageParam 分页参数
     * @param params    搜索参数
     * @return PageResult<GoodsUnitListVo>
     */
    @Override
    public PageResult<GoodsUnitListVo> list(PageParam pageParam, Map<String, String> params) {
        Integer page = pageParam.getPageNo();
        Integer limit = pageParam.getPageSize();

        QueryWrapper<GoodsUnit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc(Arrays.asList("sort", "id"));

        goodsUnitMapper.setSearch(queryWrapper, params, new String[]{
                "like:name:str",
                "=:sort:int",
        });

        IPage<GoodsUnit> iPage = goodsUnitMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<GoodsUnitListVo> list = new LinkedList<>();
        for (GoodsUnit item : iPage.getRecords()) {
            GoodsUnitListVo vo = new GoodsUnitListVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtil.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtil.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 服务单位详情
     *
     * @param id 主键参数
     * @return GoodsUnit
     */
    @Override
    public GoodsUnitDetailVo detail(Long id) {
        GoodsUnit model = goodsUnitMapper.selectOne(
                new QueryWrapper<GoodsUnit>()
                        .eq("id", id)
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        GoodsUnitDetailVo vo = new GoodsUnitDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 服务单位新增
     *
     * @param goodsUnitParam 参数
     */
    @Override
    public void add(GoodsUnitParam goodsUnitParam) {
        LambdaQueryWrapper<GoodsUnit> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GoodsUnit::getName, goodsUnitParam.getName());
        lambdaQueryWrapper.eq(GoodsUnit::getIsDelete,0);
        lambdaQueryWrapper.last("limit 1");
        GoodsUnit goodsUnit = goodsUnitMapper.selectOne(lambdaQueryWrapper);
        Assert.isNull(goodsUnit, "此单位名称已存在！");
        GoodsUnit model = new GoodsUnit();
        model.setName(goodsUnitParam.getName());
        model.setSort(goodsUnitParam.getSort());
        model.setCreateTime(TimeUtil.timestamp());
        model.setUpdateTime(TimeUtil.timestamp());
        goodsUnitMapper.insert(model);
    }

    /**
     * 服务单位编辑
     *
     * @param goodsUnitParam 参数
     */
    @Override
    public void edit(GoodsUnitParam goodsUnitParam) {
        GoodsUnit model = goodsUnitMapper.selectOne(
                new QueryWrapper<GoodsUnit>()
                        .eq("id", goodsUnitParam.getId())
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在!");
        model.setId(goodsUnitParam.getId());
        model.setName(goodsUnitParam.getName());
        model.setSort(goodsUnitParam.getSort());
        model.setUpdateTime(TimeUtil.timestamp());
        goodsUnitMapper.updateById(model);
    }

    /**
     * 服务单位删除
     *
     * @param id 主键ID
     */
    @Override
    public void del(Long id) {
        GoodsUnit model = goodsUnitMapper.selectOne(
                new QueryWrapper<GoodsUnit>()
                        .eq("id", id)
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在!");
        model.setDeleteTime(TimeUtil.timestamp());
        model.setIsDelete(GlobalConstant.DELETE);
        goodsUnitMapper.updateById(model);
    }

    @Override
    public List<GoodsUnit> listByIds(List<Long> unitIds) {
        if (CollectionUtils.isEmpty(unitIds)) {
            return Lists.newArrayList();
        }
        return super.listByIds(unitIds);
    }

    @Override
    public GoodsUnit getById(Long unitId) {
        if (null == unitId) {
            return new GoodsUnit();
        }
        return super.getById(unitId);
    }

}
